from common.email_helper import email_send_with_appendix_list
from common.logger_helper import g_wlogger
from common.pandas_helper import PandasHelper
from k3_data_report.business.cgtj_db import Cgtj
from k3_data_report.business.config import bussiness_cal_fields
from k3_data_report.finance.skjllrhs_db import SkjllrHs

__author__ = "Ypf"
_date_ = "2019/8/22-15:48"


def skjllrhs(time1):
    db_sql = bussiness_cal_fields['k3_caozuo']
    check_sql = db_sql["insert_check_sql"] % ('k3skjllr', time1)
    print (check_sql)
    check_dic = Cgtj.check_cf(check_sql)
    skjllrhs = db_sql["skjllrhs_sql"] % (time1)
    dic_skjllrhs = SkjllrHs.get_dic_skjllrhs(skjllrhs)
    if len(check_dic) == 0:
        insert_sql = db_sql["insert_skjllrhs"]
        SkjllrHs.insert_skjllrhs(dic_skjllrhs,insert_sql,time1)
    else:
        g_wlogger.winfo(f"skjllrhs-{time1}:target_full_df, all data is empty.")

def  skjllrhs_main(month):
    try:
        skjllrhs(month)
        return True
    except Exception as e:
        g_wlogger.werror(f"skjllrhs_main process error:{e} with month:{month}")
        return False

def create_df_skjllrhs(time1=None):
    db_sql = bussiness_cal_fields['k3_caozuo']
    check_sql = db_sql["insert_check_sql"] % ('k3skjllr', time1)
    print (check_sql)
    skjllrhs = db_sql["skjllrhs_sql"] % (time1)
    dic_skjllrhs = SkjllrHs.get_dic_skjllrhs(skjllrhs)
    list_end=[]
    # ghdw, hth, ckrq, cjbjdh, pp, cpmc, ggxh, bz, sfsl, xsdj, ssje, jlbl, jle, scjj, scjj1, scjj2, lkrq, bm, ywy, jsfs, sl, dwcb, cb, bhsml  23
    for key in dic_skjllrhs:
        end_tuple=tuple(dic_skjllrhs[key])
        list_end.append(end_tuple)
    columns = ["购货单位","合同号","出库日期","询报价单明细","品牌","产品名称",
               "规格型号","币种","实发数量","销售单价","实收金额","奖励比例","奖励额","四成奖金","三成奖金","三成奖金",
               "来款日期","部门","业务员","结算方式","税率","单位成本","成本","不含税毛利"]
    k3skjljlrhs_df = PandasHelper.create_dataframe(data=list_end, columns=columns)
    return k3skjljlrhs_df
def create_df_skfx(time1=None):
    db_sql = bussiness_cal_fields['k3_caozuo']
    skjllrhs = db_sql["skjllrhs_sql"] % (time1)
    dic_skjllrhs = SkjllrHs.get_dic_skjllrhs(skjllrhs)
    list_skfx=[]
    # ghdw, hth, ckrq, cjbjdh, pp, cpmc, ggxh, bz, sfsl, xsdj, ssje, jlbl, jle, scjj, scjj1, scjj2, lkrq, bm, ywy, jsfs, sl, dwcb, cb, bhsml  23
    for key in dic_skjllrhs:
        del dic_skjllrhs[key][23]
        del dic_skjllrhs[key][22]
        del dic_skjllrhs[key][21]
        end_tuple = tuple(dic_skjllrhs[key])
        list_skfx.append(end_tuple)
    columns_skfx = ["购货单位", "合同号", "出库日期", "询报价单明细", "品牌", "产品名称",
               "规格型号", "币种", "实发数量", "销售单价", "实收金额", "奖励比例", "奖励额", "四成奖金", "三成奖金", "三成奖金",
               "来款日期", "部门", "业务员", "结算方式", "税率"]
    k3skfx_df = PandasHelper.create_dataframe(data=list_skfx, columns=columns_skfx)
    return k3skfx_df



